package gttp.nspa.client;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widget.client.TextButton;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;

public class HumanForm implements IsWidget {

    protected String                  caption;
    protected int                     formWidth;
    protected int                     labelWidth;

    protected TextField               tfName;
    protected TextField               tfAddr;
    protected TextField               tfPhone;
    protected TextField               tfEmail;

    protected TextButton              btnSave;
    protected TextButton              btnCancel;

    protected VerticalLayoutContainer con;
    protected ContentPanel            pane;

    public HumanForm(String caption, int formWidth, int labelWidth) {
        this.caption = caption;
        this.formWidth = formWidth;
        this.labelWidth = labelWidth;
    }

    public Widget asWidget() {
        if (pane == null) {
            pane = createContentPane(caption, formWidth);
            con = new VerticalLayoutContainer();
            pane.add(con);
            createFormFields(con);
            createFormCommands(pane);
        }
        return pane;
    }

    protected ContentPanel createContentPane(String caption, int width) {
        FramedPanel pane = new FramedPanel();
        pane.setHeadingText(caption);
        pane.setWidth(width);
        pane.setBodyStyle("background: none; padding: 5px");
        return pane;
    }

    protected void createFormFields(VerticalLayoutContainer con) {
        FieldLabel fl;

        tfName = new TextField();
        fl = new FieldLabel(tfName, "Tên");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfAddr = new TextField();
        fl = new FieldLabel(tfAddr, "Địa chỉ");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfPhone = new TextField();
        fl = new FieldLabel(tfPhone, "Điện thoại");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        tfEmail = new TextField();
        fl = new FieldLabel(tfEmail, "Email");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));
    }

    protected void createFormCommands(ContentPanel pane) {
        btnSave = new TextButton("Save");
        btnCancel = new TextButton("Cancel");

        pane.addButton(btnSave);
        pane.addButton(btnCancel);
    }
}
